﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OLMS.DAL;
using OLMS.BLL;

namespace OLMS
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if(Request["t"]!=null && Request["t"] == "reg")
                {
                    pnlLogin.Visible = false;
                    pnlRegiester.Visible = true;
                }
                else
                {
                    pnlRegiester.Visible = false;
                    pnlLogin.Visible = true;
                }
            }
        }


        protected void btnRegister_Click(object sender, EventArgs e)
        {
           
            Users user = new Users();
            user.LoginId = txtRegLoginId.Text;
            user.LoginPwd = txtRegLoginPwd.Text;
            user.UserName = txtRegUserName.Text;
            user.Phone = txtRegPhone.Text;
            user.RoleId = 2; //默认情况下，用户角色是普通用户
            user.UserStatus = 1;  //用户状态为启用
            user.DateCreated = DateTime.Now;

            if (UserBLL.Register(user))
            {
                Session["user"] = user;
                Response.Redirect("Index.aspx");
            }
            else
            {
                Response.Write("<script>alert('用户信息输入有错误!')</script>");
            }
        }

        protected void btnReturn_Click(object sender, EventArgs e)
        {
            pnlRegiester.Visible = false;
            pnlLogin.Visible = true;
        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            string loginId = txtLoginId.Text;
            string loginPwd = txtLoginPwd.Text;
            string validCode = txtValidateCode.Text;
            string snCode = Session["sn"] as string;
            if (snCode == validCode)
            {
                Users user = UserBLL.Login(loginId, loginPwd);
                if (user != null)
                {
                    Session["user"] = user;
                    if (user.RoleId != 1)
                    {
                        Response.Redirect("~/Index.aspx");
                    }
                    else
                    {
                        Response.Redirect("~/Admin/OrdersManager.aspx");
                    }

                }
                else
                {
                    Response.Write("<script>alert('登录账号或登录密码错误!')</script>");
                }
            }
            else
            {
                Response.Write("<script>alert('验证码错误!')</script>");
            }
        }

        protected void btnReg_Click(object sender, EventArgs e)
        {
            pnlLogin.Visible = false;
            pnlRegiester.Visible = true;
        }
    }
}